import { instrDelete, instrDisable, instrInfo, instrList, instrSave } from '@/api/knowledge'
import { ref } from 'vue'

/**
 * 获取说明书列表
 */
export const useGetManualList = () => {
  // 获取列表加载
  const getListLoading = ref(false)

  // 获取列表请求参数
  const getListParam = ref({
    name: '',
    limit: 50,
    page: 1,
  })

  // 说明书列表
  const manualList = ref<Array<ManualListItem>>([])

  // 列表总条数
  const listTotal = ref(0)

  /**
   * 获取列表
   */
  const getList = async () => {
    getListLoading.value = true
    const res = await instrList(getListParam.value)
    manualList.value = res.data.data.rows
    listTotal.value = Number(res.data.data.total)
    getListLoading.value = false
  }

  return {
    listTotal,
    manualList,
    getListParam,
    getListLoading,
    getList,
  }
}

/**
 * 批量删除说明书
 */
export const useManualDelByIds = () => {
  // 删除加载
  const delLoading = ref(false)

  /**
   * 删除
   * @param {Array<string>} ids - 说明书id集合
   */
  const del = async (ids: Array<string>) => {
    delLoading.value = true
    const res = await instrDelete(ids)
    delLoading.value = false
    return res.data
  }

  return {
    delLoading,
    del,
  }
}

/**
 * 说明书批量启用
 */
export const useManualEnableByIds = () => {
  // 启用加载
  const enableLoading = ref(false)

  /**
   * 启用
   * @param {Array<string>} ids - 说明书id集合
   */
  const enable = async (ids: Array<string>) => {
    enableLoading.value = true
    const res = await instrDisable(ids, false)
    enableLoading.value = false
    return res.data
  }

  return {
    enableLoading,
    enable,
  }
}

/**
 * 说明书批量禁用
 */
export const useManualDisableByIds = () => {
  // 禁用加载
  const disableLoading = ref(false)

  /**
   * 禁用
   * @param {Array<string>} ids - 说明书id集合
   */
  const disable = async (ids: Array<string>) => {
    disableLoading.value = true
    const res = await instrDisable(ids, true)
    disableLoading.value = false
    return res.data
  }

  return {
    disableLoading,
    disable,
  }
}

/**
 * 获取说明书信息
 */
export const useGetManualInfo = () => {
  // 获取信息加载
  const getInfoLoading = ref(false)

  // 说明书信息
  const manualInfo = ref<ManualPreviewInfo>({
    content: '',
    id: '',
    image: '',
    name: '',
    productInstrBookVos: [],
    status: false,
    visibilityType: 1,
    updateTime: '',
  })

  /**
   * 获取信息
   * @param {String} id - 说明书id
   */
  const getInfo = async (id: string) => {
    getInfoLoading.value = true
    const res = await instrInfo(id)
    manualInfo.value = res.data.data.row
    getInfoLoading.value = false
  }

  return {
    manualInfo,
    getInfoLoading,
    getInfo,
  }
}

/**
 * 说明书表单
 */
export const useManualForm = () => {
  // 表单保存加载
  const formSaveLoading = ref(false)

  // 表单
  const form = ref<KnownAPIRequest.ManualForm>({
    content: '',
    id: '',
    image: '',
    name: '',
    productInstrBookVos: [],
    status: false,
    visibilityType: 1,
  })

  /**
   * 重置表单
   */
  const resetForm = () => {
    form.value = {
      content: '',
      id: '',
      image: '',
      name: '',
      productInstrBookVos: [],
      status: false,
      visibilityType: 1,
    }
  }

  /**
   * 表单保存
   */
  const formSave = async () => {
    formSaveLoading.value = true
    const res = await instrSave(form.value)
    formSaveLoading.value = false
    return res.data
  }

  return {
    form,
    formSaveLoading,
    formSave,
    resetForm,
  }
}
